const serIp = "http://192.168.0.194:8888"
wx.baseUri = serIp
const codemap = 
  {
  code200: 200,
  content200: "正常",
  code402: 402,
  content402: "验证错误",
  code400: 400,
  content400: "业务错误",
  code500500: 500500,
  content500500: "登录超时",
}

class Http {
  request(params) {
    
    wx.showLoading({title: '加载中…'})
    wx.request({
      url: serIp + params.url,
      data: params.data,
      header: {
        'content-type':
        'application/json;charset=utf-8',
        'Cookie': wx.getStorageSync('cookieKey'),
        ... params.header
      },
      method: params.method || 'GET',
      success: (res) => {
        wx.setStorageSync("cookieKey", res.header["Set-Cookie"]);
        if (res.data.code == codemap.code200 || res.data.code == codemap.code400) {
          params.success(res.data)
          
        } else if (res.data.code==codemap.code402) {
          wx.showToast({
            title: res.data.msg||'联系管理员',
            icon: 'error',
            mask: true,
            duration:1000,
          })
        } else if (res.data.code == codemap.code500500) {
          wx.navigateTo({
            url: '/pages/login/login',
          })
          setTimeout(()=>{
        
            wx.showToast({
              title: res.data.msg||'登录超时重新登录',
              icon: 'error',
              mask: true,
              duration:1500,
            })
           }, 1500)
  
        }else {
          try {
            params.fail(res)
          } catch (error) {
            console.error(res)
            wx.showToast({
              title: res.data.msg||'联系管理员',
              icon: 'error',
              mask: true,
              duration:1000,
            })
          }
          wx.hideLoading()
        }
      },
      fail: (res) => {
        wx.hideLoading()
      },
      complete: (res) => {
        
      },
    })

  }
}
const http = new Http()
export {http}